<template>
    <div>
        <div class="title">可疑报送统计数据</div>
        <bfe-form :model="formObj" ref="formObj" label-position="top">
            <vxe-table :data="formObj.list" border style="width: 100%">
                <vxe-table-column title="项目" prop="prj">
                    <template #default="{ row, rowIndex }">
                        <div>{{ formObj.list[rowIndex].prjDesc }}</div>
                    </template>
                </vxe-table-column>
                <vxe-table-column title="本期" prop="cnrrd">
                    <template #default="{ row, rowIndex }">
                        <bfe-form-item label :rules="rowIndex === 1 ? formRules.txnAmtExgus : formRules.txnDnum"
                            :prop="`list.${rowIndex}.cnrrd`">
                            <bfe-input v-if="type !== 'view'" v-model="formObj.list[rowIndex].cnrrd" size="small"
                                type="input" :maxlength="rowIndex === 1 ? 20 : 12"
                                :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
                            <div v-else>{{ formObj.list[rowIndex].cnrrd && formObj.list[rowIndex].cnrrd !== 'null' ?
                                formObj.list[rowIndex].cnrrd : '' }}</div>
                        </bfe-form-item>
                    </template>
                </vxe-table-column>
                <vxe-table-column title="上期" prop="ctp">
                    <template #default="{ row, rowIndex }">
                        <bfe-form-item label :rules="rowIndex === 1 ? formRules.txnAmtExgus : formRules.txnDnum"
                            :prop="`list.${rowIndex}.ctp`">
                            <bfe-input v-if="type !== 'view'" v-model="formObj.list[rowIndex].ctp" size="small"
                                :disabled="formObj.list[rowIndex].hasCtpValue" type="input"
                                :maxlength="rowIndex === 1 ? 20 : 12"
                                :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
                            <div v-else>{{ formObj.list[rowIndex].ctp && formObj.list[rowIndex].ctp !== 'null' ?
                                formObj.list[rowIndex].ctp : '' }}</div>
                        </bfe-form-item>
                    </template>
                </vxe-table-column>
                <vxe-table-column title="去年同期" prop="lyAtst">
                    <template #default="{ row, rowIndex }">
                        <bfe-form-item label :rules="rowIndex === 1 ? formRules.txnAmtExgus : formRules.txnDnum"
                            :prop="`list.${rowIndex}.lyAtst`">
                            <bfe-input v-if="type !== 'view'" v-model="formObj.list[rowIndex].lyAtst" size="small"
                                :disabled="formObj.list[rowIndex].hasLyAtstValue" type="input"
                                :maxlength="rowIndex === 1 ? 20 : 12"
                                :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
                            <div v-else>{{ formObj.list[rowIndex].lyAtst && formObj.list[rowIndex].lyAtst !== 'null' ?
                                formObj.list[rowIndex].lyAtst : '' }}</div>
                        </bfe-form-item>
                    </template>
                </vxe-table-column>
            </vxe-table>
        </bfe-form>
    </div>
</template>

<script>
import { mapState } from 'vuex';
import moment from 'moment'

export default {
    props: {
        type: {
            type: String,
            default: ''
        },
        date: {
            type: String,
            default: ''
        },
    reqOrgNo: {
      type: String,
      default: null,
    }
    },
    data() {
        return {
            formObj: {
                list: [],
            },
            formRules: {
                txnDnum: [{
                    validator: (rule, value, callback) => {
                        if (value === '' || value === null || value === undefined) {
                            // 如果字段非必填，空值通过；如果是必填，请在 rules 中加 required: true
                            return callback();
                        }
                       const str = String(value).trim();
                        const regex = /^(?:0|[1-9]\d*)$/;

                        if (regex.test(str)) {
                            callback();
                        } else {
                            callback(new Error(this.$t('stm.branchTransReport.pleaseInputInt')))
                        }
                    },
                    required: false,
                    trigger: 'blur'
                }],
                txnAmtExgus: [{
                    validator: (rule, value, callback) => {
                        if (value === '' || value === null || value === undefined) {
                            // 如果字段非必填，空值通过；如果是必填，请在 rules 中加 required: true
                            return callback();
                        }

                        const str = String(value).trim();

                        // 正则：0 或 [1-9][最多16位数字] + 可选小数部分（1~3位）
                        const regex = /^(?:0|(?:[1-9]\d{0,15}))(?:\.\d{1,3})?$/;

                        if (regex.test(str)) {
                            callback(); // 校验通过
                        } else {
                            callback(new Error('整数最多16位，小数最多3位'));
                        }
                    },
                    required: false,
                    trigger: 'blur'
                }],
            },
        }
    },
    computed: {
        ...mapState({
            language: (state) => state.base.language,
            userInfo: (state) => state.base.userInfo,
        }),
    },
    created() {
        this.initData()
    },
    methods: {
        // 初始化数据
        initData() {
            if (this.type !== "add") {
                const params = {
                    optCode: 'STM002GET1805',
                    payload: {
                        rptOrgNo: this.reqOrgNo,
                        rptpd: this.date,
                    },
                    pageInfo: {
                        pageNo: 1,
                        pageSize: 10
                    }
                }
                this.$p2svr(this.$api['_01_TSTM02MONRPT1800'], 'RequestPoAuth', { ...params }).then(res => {
                    //console.log(res.payload, '报送详情打印');
                    // 如果详情没查到数据 新增空数据
                    if (!res.payload || !Array.isArray(res.payload) || res.payload.length === 0) {
                        if (this.type !== 'view') {
                            this.formObj.list = [
                                { prj: '1', prjDesc: '向监管机构报送可疑交易笔数', cnrrd: '', ctp: '', lyAtst: '', hasCtpValue: false, hasLyAtstValue: false },
                                { prj: '2', prjDesc: '向监管机构报送可疑交易涉及金额（折人民币：元）', cnrrd: '', ctp: '', lyAtst: '', hasCtpValue: false, hasLyAtstValue: false },
                                { prj: '3', prjDesc: '向监管机构报送可疑交易涉及金额机构客户数', cnrrd: '', ctp: '', lyAtst: '', hasCtpValue: false, hasLyAtstValue: false },
                                { prj: '4', prjDesc: '向监管机构报送可疑交易设计对公客户数（不含金融机构）', cnrrd: '', ctp: '', lyAtst: '', hasCtpValue: false, hasLyAtstValue: false },
                                { prj: '5', prjDesc: '向监管机构报送可疑交易设计对私客户数', cnrrd: '', ctp: '', lyAtst: '', hasCtpValue: false, hasLyAtstValue: false },
                            ]
                        }
                    } else {
                        this.formObj.list = res.payload.map(item => ({
                            ...item,
                            hasCtpValue: item.ctp == '0' || !!item.ctp, // 如果已有值，视为已输入
                            hasLyAtstValue: item.lyAtst == '0' || !!item.lyAtst  // 如果已有值，视为已输入
                        }));
                    }
                })
            } else {
                const params = {
                    optCode: 'STM002GET1805',
                    payload: {
                        rptOrgNo: this.userInfo.userAhrOrg,
                        rptpd: this.date,
                        queryFrom: 'add'
                    },
                    pageInfo: {
                        pageNo: 1,
                        pageSize: 10
                    }
                }
                this.$p2svr(this.$api['_01_TSTM02MONRPT1800'], 'RequestPoAuth', { ...params }).then(res => {
                    if (!res.payload || !Array.isArray(res.payload) || res.payload.length === 0) {
                        this.formObj.list = [
                            { prj: '1', prjDesc: '向监管机构报送可疑交易笔数', cnrrd: '', ctp: '', lyAtst: '', hasCtpValue: false, hasLyAtstValue: false },
                            { prj: '2', prjDesc: '向监管机构报送可疑交易涉及金额（折人民币：元）', cnrrd: '', ctp: '', lyAtst: '', hasCtpValue: false, hasLyAtstValue: false },
                            { prj: '3', prjDesc: '向监管机构报送可疑交易涉及金额机构客户数', cnrrd: '', ctp: '', lyAtst: '', hasCtpValue: false, hasLyAtstValue: false },
                            { prj: '4', prjDesc: '向监管机构报送可疑交易设计对公客户数（不含金融机构）', cnrrd: '', ctp: '', lyAtst: '', hasCtpValue: false, hasLyAtstValue: false },
                            { prj: '5', prjDesc: '向监管机构报送可疑交易设计对私客户数', cnrrd: '', ctp: '', lyAtst: '', hasCtpValue: false, hasLyAtstValue: false },
                        ]
                    } else {
                        this.formObj.list = res.payload.map(item => ({
                            ...item,
                            hasCtpValue: item.ctp == '0' || !!item.ctp, // 如果已有值，视为已输入
                            hasLyAtstValue: item.lyAtst == '0' || !!item.lyAtst  // 如果已有值，视为已输入
                        }));
                    }
                })
            }

        },
        validateForm() {
            let flag = false
            this.$refs.formObj.validate((ok) => {
                if (ok) {
                    flag = true
                } else {
                }
            })
            return flag
        },
    }
}
</script>

<style lang="less" scoped>
.title {
    font-size: 18px;
    color: rgba(0, 0, 0, 0.85);
    font-weight: 600;
    margin: 10px 0;
}
</style>